#import "GameController.h"
#import "Box.h"
#import "TwoDArray.h"

const NSUInteger kMaxRowAndColIndex = 3;
const NSUInteger kMinRowAndColIndex = 0;

const NSUInteger kMaxRows = 4;
const NSUInteger kMaxCols = 4;
BOOL hasChanged;


//Class extension
@interface GameController ()



@property(readwrite) TwoDArray* gamePlayground;
@property(readwrite) NSUInteger points;


@end


//Implementation
@implementation GameController

    //Adding points
-(void)addPoints:(NSUInteger)points{
    self.points += points*2;
}

-(void)removeAllPoints{
    self.points = 0;
}

    //Standard Messages
            //Generate random box
-(void) generateBoxAtRandomEmptyPlace{

    BOOL hasEmptyBox = NO;

    
        NSUInteger row = arc4random_uniform(kMaxRows);
        NSUInteger col = arc4random_uniform(kMaxCols);
    
    NSUInteger rowSample = row;
    NSUInteger colSample = col;
    
    if (!hasEmptyBox) {
        for (NSUInteger indexRow = rowSample; indexRow <= kMaxRowAndColIndex; indexRow++) {
            for (NSUInteger indexCol = colSample; indexCol <= kMaxRowAndColIndex; indexCol++) {
                if (![self.gamePlayground objectAtRow:indexRow column:indexCol]) {
                    [self.gamePlayground insertObject:[self generateBoxWithValueTwoOrFour] atRow:indexRow column:indexCol];
                    NSLog(@" Kuttika red: %ld kolona: %ld",indexRow,indexCol);
                    return;
                }
            }
            colSample = kMinRowAndColIndex;
        }
        hasEmptyBox = YES;
    }
    if(hasEmptyBox){
        for (NSUInteger indexRow = 0; indexRow <= rowSample; indexRow++) {
            for (NSUInteger indexCol = 0; indexCol <= kMaxRowAndColIndex; indexCol++){
                if (![self.gamePlayground objectAtRow:indexRow column:indexCol]) {
                    [self.gamePlayground insertObject:[self generateBoxWithValueTwoOrFour] atRow:indexRow column:indexCol];
                    NSLog(@" Kuttika red: %ld kolona: %ld",indexRow,indexCol);
                    return;
                }
            }
        }
    }
}

            //Chance of generating 2 or 4 box
-(Box*)generateBoxWithValueTwoOrFour{
    NSUInteger value = arc4random_uniform(8);
    Box* boxche = [[Box alloc] init];
    switch (value) {
        case 0:
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
              boxche = [[Box alloc]initWithValue:two];
            break;
        case 7:
              boxche = [[Box alloc]initWithValue:four];
            break;
        default:
            break;
    }
    return boxche;
}
    //Singletoned
static BOOL isInited = NO;

+(id) sharedGameController {
    static GameController* p = nil;
    if (p==nil)
        p = [[super alloc] init];
    
    return p;
}

+(id)alloc {
    return [GameController sharedGameController];
}

-(id)copy {
    return [GameController sharedGameController];
}

-(id)mutableCopy {
    return [GameController sharedGameController];
}



    //Designated Initialiser
- (instancetype)init
{
    if (isInited)
        return  self;
    
    self = [super init];
    if (self) {
        self.gamePlayground = [[TwoDArray alloc] initWithRows:kMaxRows columns:kMaxCols];
        isInited = YES;

    }
    return self;
}





@end
